package com.yungam.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Component
public class DateUtils {

    private final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");
    private final DateTimeFormatter DF = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * 获取今日的时间范围
     */
    public String[] getTodayRange() {
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(DF);
        String start = format + " 00:00:00",
                end = format + " 23:59:59";

        return new String[]{ start, end };
    }

    /**
     * 获取昨日的时间范围
     */
    public String[] getYesterdayRange() {
        LocalDateTime lastDay = LocalDateTime.now().minusDays(1);
        String format = lastDay.format(DF);
        String start = format + " 00:00:00",
                end = format + " 23:59:59";

        return new String[]{ start, end };
    }

    /**
     * 获取指定前 n 天的起始时间
     */
    public long[] getLongRangeByNum(int n) {
        LocalDateTime target = LocalDateTime.now().minusDays(n);
        String format = target.format(DF);
        String start = format + " 00:00:00",
                end = format + " 23:59:59";

        long l = 0L, r = 0L;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            l = sdf.parse(start).getTime();
            r = sdf.parse(end).getTime();
        } catch (Exception e) {
            log.error(String.valueOf(e));
        }

        return new long[]{ l, r };
    }
}
